package com.gzhryc.shared_device.oem.micro.queue.handlers.order;

import com.gzhryc.common.event.AbsDelayHandler;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.shared_device.base.models.IDeviceOrder;
import com.gzhryc.shared_device.bxscn.code.services.BXSCNOrderService;
import com.gzhryc.shared_device.bxscn.code.services.BXSCNShopOrderService;
import com.gzhryc.shared_device.cdb.code.services.CDBOrderService;
import com.gzhryc.shared_device.cdcz.code.services.CDCZOrderService;
import com.gzhryc.shared_device.oem.code.business.EBusinessType;
import com.gzhryc.shared_device.oem.code.order.event.OrderCancelEvent;
import com.gzhryc.shared_device.oem.code.statistics.services.PlaceDayStatisticsService;
import com.gzhryc.shared_device.oem.code.statistics.services.ProjectDayStatisticsService;
import com.gzhryc.shared_device.pcl.code.services.PCLOrderService;

public class OrderCancelEventHandler extends AbsDelayHandler<OrderCancelEvent> {

    static Logger log = Logger.getLogger(OrderFinishEventHandler.class);

    @Override
    public boolean execute(OrderCancelEvent event) {
        log.info("订单撤销事件：{{0:json}}", event);
        try {
            IDeviceOrder deviceOrder = null;
            if (EBusinessType.CDCZ.index().equals(event.getBusinessType())) {
                deviceOrder = CDCZOrderService.self(event.getCreateDate()).getByTradeNo(event.getTradeNo());
            } else if (EBusinessType.CDB.index().equals(event.getBusinessType())) {
                deviceOrder = CDBOrderService.self(event.getCreateDate()).getByTradeNo(event.getTradeNo());
            } else if (EBusinessType.PCL.index().equals(event.getBusinessType())) {
                deviceOrder = PCLOrderService.self(event.getCreateDate()).getByTradeNo(event.getTradeNo());
            } else if (EBusinessType.BXSCN.index().equals(event.getBusinessType())) {
                deviceOrder = BXSCNOrderService.self(event.getCreateDate()).getByTradeNo(event.getTradeNo());
            } else if (EBusinessType.BXSCN_SHOP.index().equals(event.getBusinessType())) {
                deviceOrder = BXSCNShopOrderService.self(event.getCreateDate()).getByTradeNo(event.getTradeNo());
            } else {
                log.error("暂不支持的设备功能分成");
                return true;
            }

            if (deviceOrder == null) {
                log.error("未找到设备订单信息，设备功能：{{0}}，订单号：{{1}}", event.getBusinessType(), event.getTradeNo());
                return true;
            }

            //项目日统计
            ProjectDayStatisticsService.self().addOneCancelData(deviceOrder.getCancelDate(), event.getRefundMoney(), deviceOrder.getFinishDate());
            //网点日统计
            PlaceDayStatisticsService.self().addOneCancelData(deviceOrder.getPlaceId(), deviceOrder.getPlaceName()
                    , deviceOrder.getPlaceAreaName(), deviceOrder.getCancelDate(), event.getRefundMoney(), deviceOrder.getFinishDate());
            //地区日统计
            //TODO 重新设计
            //CityAreaDayStatisticsService.self().addOneCancelData(deviceOrder.getAreaCode(),deviceOrder.getCancelDate(),event.getRefundMoney(),deviceOrder.getFinishDate());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return true;
    }
}
